package logic

import (
	"context"
	"time"
	"zero-cloud-disk/api/helper"
	"zero-cloud-disk/api/models"

	"zero-cloud-disk/api/internal/svc"
	"zero-cloud-disk/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRepositoryStorageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserRepositoryStorageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRepositoryStorageLogic {
	return &UserRepositoryStorageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserRepositoryStorageLogic) UserRepositoryStorage(req *types.UserRepositoryStorageReq, userIdentity string) (resp *types.UserRepositoryStorageRes, err error) {
	rq := &models.UserRepository{
		Identity:           helper.GetUUID(),
		UserIdentity:       userIdentity,
		ParentId:           req.ParentId,
		RepositoryIdentity: req.RepositoryIdentity,
		Ext:                req.Ext,
		Name:               req.Name,
		CreatedAt:          time.Time{},
		UpdatedAt:          time.Time{},
		DeletedAt:          time.Time{},
	}
	_, err = l.svcCtx.Engine.Insert(rq)
	if err != nil {
		return nil, err
	}

	return &types.UserRepositoryStorageRes{Identity: userIdentity}, nil
}
